package com.cs.framework.jdbc.mybatis3.support;

import org.springframework.dao.support.DaoSupport;

import com.cs.framework.jdbc.mybatis3.exception.MyBatisExceptionHelper;
import com.cs.framework.jdbc.mybatis3.session.SelectSession;
import com.cs.framework.jdbc.mybatis3.session.SelectSessionFactory;
import com.cs.framework.jdbc.mybatis3.session.SelectSessionTemplate;

/**
 * 对MyBatis数据库访问提供支持
 * 
 * @author hedongzhou
 * 
 */
public class MyBatisDaoSupport extends DaoSupport {

	/**
	 * 工厂
	 */
	private SelectSessionFactory selectSessionFactory;

	/**
	 * 会话
	 */
	private SelectSession selectSession;

	public void setSelectSessionFactory(SelectSessionFactory selectSessionFactory) {
		this.selectSessionFactory = selectSessionFactory;
	}

	/**
	 * 检查配置
	 */
	protected void checkDaoConfig() throws IllegalArgumentException {
		if (selectSessionFactory == null) {
			throw new IllegalArgumentException(MyBatisExceptionHelper.getMessage().getMessage("M009"));
		}
	}

	/**
	 * 初始化
	 */
	protected void initDao() throws Exception {
		selectSession = new SelectSessionTemplate(selectSessionFactory);
	}

	/**
	 * 获取回话模板
	 * 
	 * @return
	 */
	public SelectSession getMyBatisTemplate() {
		return selectSession;
	}

	/**
	 * 忽略
	 */
	@Deprecated
	public void close() {
	}

}
